/*************************************************
*created by anchengan in 2024years 08months 06days
*This program is used to test message sending
*************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <signal.h>
#include <unistd.h> 
int sock;
void handle_exit_sigint(int sig){
    close(sock);
    printf("autoai_yolov5 exit,return 0\n");
    exit(0);
}
//check if IP valid
int isValidIP(char *ip){
    struct sockaddr_in sa;
    int result=inet_pton(AF_INET,ip,&(sa.sin_addr));
    return result != 0;
}

//check if PORT valid
int isValidPORT(char *port){
    for(int i=0;port[i]!='\0';i++){
        if(!isdigit(port[i])){
            return 0;
        }
    }
    int portNum=atoi(port);
    return portNum>0 && portNum<=65535;
}
int main(int argc,char *argv[]){
    signal(SIGINT,handle_exit_sigint);
    if (argc !=3){
        fprintf(stderr,"Usage:%s <IP address> <port>\n",argv[0]);
        exit(EXIT_FAILURE);
    }
    char *ip = argv[1];
    char *port = argv[2];
    if (!isValidIP(ip)){
        fprintf(stderr,"Invalid IP address:%s\n",ip);
        exit(EXIT_FAILURE);
    }
    if (!isValidPORT(port)){
        fprintf(stderr,"Invalid port number:%s\n",port);
        exit(EXIT_FAILURE);
    }
    printf("This program is used to test message sending\n");
    
    struct sockaddr_in serv_addr;
    char words_test[1024]={0};
    char buffer[1024]={0};
    char* words_tran;
    int valread;
    //create socket
    sock=socket(AF_INET,SOCK_STREAM,0);
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_port=htons(atoi(port));
    serv_addr.sin_addr.s_addr=inet_addr(ip);

    connect(sock,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
    //send message
    words_tran="autoai_startyolov5";
    strcpy(words_test,words_tran);
    send(sock,words_test,strlen(words_test),0);
    printf("words test sent\n");
    //receive message
    valread=read(sock,buffer,1024);
    printf("%s\n",buffer);
    close(sock);
    sock=socket(AF_INET,SOCK_STREAM,0);
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_port=htons(atoi(port));
    serv_addr.sin_addr.s_addr=inet_addr(ip);

    connect(sock,(struct sockaddr *)&serv_addr,sizeof(serv_addr));

    words_tran="autoai_ipportsetyolov5-127.0.0.2:6998";
    strcpy(words_test,words_tran);
    send(sock,words_test,strlen(words_test),0);
    printf("yolov5 service started successfully\n");
    //receive message
    valread=read(sock,buffer,1024);
    close(sock);
    printf("%s\n",buffer);
    while(1){
	sock=socket(AF_INET,SOCK_STREAM,0);
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(atoi(port));
        serv_addr.sin_addr.s_addr=inet_addr(ip);
	connect(sock,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
        send(sock,words_test,strlen(words_test),0);   
	valread=read(sock,buffer,1024);
	close(sock);
	usleep(500000);  
    }
    return 0;
}
 
